package com.jdew.thkj.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.adapter.OrderDetailAdapter;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.bean.OrderBean;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.utils.DeviceUtils;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.utils.ToastUtils;
import com.jdew.thkj.utils.WeiboDialogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author Administrator
 */
public class OrderDetailActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.message)
    TextView message;
    @Bind(R.id.view)
    LinearLayout view;
    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.view2)
    RelativeLayout view2;
    public SharedPreferences sp;
    public SharedPreferences.Editor editor;
    private Dialog mWeiboDialog;
    private String orderNo;
    private OrderDetailAdapter adapter;
    private List<OrderBean.DataBean> list = new ArrayList<>();

    @Override
    public int bindLayout() {
        return R.layout.activity_order_detail;
    }

    @Override
    public void initView(View view) {
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        title.setText("查询结果");
        sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        orderNo = getIntent().getStringExtra("orderNo");
        adapter = new OrderDetailAdapter(this, list);
        setRcycleview(recycleView, adapter);
        GetOrder();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
            default:
                break;
        }
    }

    public void GetOrder() {
        if (!DeviceUtils.isNetworkAvalible(this)) {
            if (!isFinishing()) {
                ToastUtils.getInstance(this).showMessage("网络连接错误请检查");
            }
            return;
        }
        mWeiboDialog = WeiboDialogUtils.createLoadingDialog(this);
        String timesamp= RegexValidateUtils.getTime13();
        String numsamp=RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance(AppConfig.BASE_URL).createReq(RequestApi.class).GetOrderNew(orderNo, sp.getString("userId", ""), RegexValidateUtils.getUniquePsuedoID(),timesamp,numsamp,MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp+numsamp+RegexValidateUtils.getUniquePsuedoID()+sp.getString("SignToken","")+"Order"+orderNo+"UserId"+sp.getString("userId", ""))).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<OrderBean>() {
            @Override
            public void onCompleted() {
                WeiboDialogUtils.closeDialog(mWeiboDialog);
            }

            @Override
            public void onError(Throwable e) {
                WeiboDialogUtils.closeDialog(mWeiboDialog);
            }

            @Override
            public void onNext(OrderBean bean) {
                WeiboDialogUtils.closeDialog(mWeiboDialog);
                if (bean.getCode().equals("0")) {
                    view.setVisibility(View.VISIBLE);
                    view2.setVisibility(View.GONE);
                    message.setText(bean.getMessage());
                    list = bean.getData();
                    adapter.setDataList(list);
                } else if (bean.getCode().equals("-1")) {
                    view.setVisibility(View.GONE);
                    recycleView.setVisibility(View.GONE);
                    view2.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}
